package com.heima.controller;

import com.heima.pojo.dto.PartnerCollectDto;
import com.heima.pojo.vo.AmountCollectVo;
import com.heima.pojo.vo.PartnerCollectPageVo;
import com.heima.pojo.vo.SkuVo;
import com.heima.service.ReportService;
import com.heima.service.SkuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@Api(tags = "报告接口")
@RequestMapping("/api/order-service/report")
@Slf4j
public class ReportController {

    @Autowired
    private ReportService reportService;
    @Autowired
    private SkuService skuService;

    /**
     * 当月订单总量
     */
    @GetMapping("/orderCount")
    @ApiOperation("订单总量")
    public Integer monthOrderCount(String start, String end){
        log.info("start{},end{}",start,end);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date first = null;
        Date last = null;
        try {
            first = simpleDateFormat.parse(start);
            last = simpleDateFormat.parse(end);
        } catch (ParseException e) {
            log.info("日期信息转换失败" ,e);
        }
        Integer count = reportService.monthOrderCount(first, last);
        return count;
    }

    /**
     * 月销售额
     */
    @GetMapping("/orderAmount")
    @ApiOperation("销售额")
    public Long monthOrderAmount(String start, String end){
        log.info("start{},end{}",start,end);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date first = null;
        Date last = null;
        try {
            first = simpleDateFormat.parse(start);
            last = simpleDateFormat.parse(end);
        } catch (ParseException e) {
            log.info("日期信息转换失败" ,e);
        }
        Long amount = reportService.monthOrderAmount(first, last);
        return amount;
    }

    /**
     *销售额统计
     */
    @GetMapping("/amountCollect/{collectType}/{start}/{end}")
    @ApiOperation("统计销售额")
    public AmountCollectVo getAmountCollect(
            @PathVariable Integer collectType,
            @PathVariable String start,
            @PathVariable String end){
        log.info("type:{},start:{},end:{}",collectType,start,end);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date first = null;
        Date last = null;
        try {
            first = simpleDateFormat.parse(start);
            last = simpleDateFormat.parse(end);
        } catch (ParseException e) {
            log.info("日期信息转换失败" ,e);
        }
        AmountCollectVo vo = reportService.AmountCollectVo(collectType,first,last);
        return vo;
    }

    /**
     * 销售量分布
     */
    @GetMapping("/regionCollect/{start}/{end}")
    public AmountCollectVo regionCollect(@PathVariable String start, @PathVariable String end){
        log.info("start{},end{}",start,end);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date first = null;
        Date last = null;
        try {
            first = simpleDateFormat.parse(start);
            last = simpleDateFormat.parse(end);
        } catch (ParseException e) {
            log.info("日期信息转换失败" ,e);
        }
        AmountCollectVo vo = reportService.regionCollect(first, last);
        return vo;

    }

    /**
     * 获取前几的商品
     */
    @GetMapping("/skuTop/{topValue}/{start}/{end}")
    public List<SkuVo> queryTopSku(@PathVariable Integer topValue,
                                   @PathVariable String start,
                                   @PathVariable String end){
        log.info("start{},end{},value:{}",start,end,topValue);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date first = null;
        Date last = null;
        try {
            first = simpleDateFormat.parse(start);
            last = simpleDateFormat.parse(end);
        } catch (ParseException e) {
            log.info("日期信息转换失败" ,e);
        }
        List<SkuVo> skuVos = skuService.queryTopSku(topValue,first,last);
        return skuVos;
    }

    /**
     * 获取一定日期范围之内的合作商分成汇总数据
     */
    @GetMapping("/partnerCollect")
    public PartnerCollectPageVo partnerCollect(PartnerCollectDto dto){
        log.info("前端的参数:{}",dto);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date first = null;
        Date last = null;
        try {
            first = simpleDateFormat.parse(dto.getStart());
            last = simpleDateFormat.parse(dto.getEnd());
        } catch (ParseException e) {
            log.info("日期信息转换失败" ,e);
        }
        dto.setFirst(first);
        dto.setLast(last);
        PartnerCollectPageVo pageVo = reportService.partnerCollect(dto);
        return pageVo;
    }

    /**
     * 获取一定时间范围之内的分成总数
     */
    @GetMapping("/totalBill")
    public String getTotalBill(String start,String end,Long id){
        log.info("start:{},end:{},id:{}",start,end,id);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date first = null;
        Date last = null;
        try {
            first = simpleDateFormat.parse(start);
            last = simpleDateFormat.parse(end);
        } catch (ParseException e) {
            log.info("日期信息转换失败" ,e);
        }
        log.info("first:{},last:{},id:{}",first,last,id);
        return reportService.getTotalBill(first,last,id).toString();
    }

}
